import java.util.*;

/**
 * @author LKQ
 * @date 2022/3/22 20:32
 * @description 思路 遍历生成, 每一次遍历，都需要借助双指针来找到相同的一组数字，然后赋值给str
 */
public class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();
        solution.countAndSay(8);
    }
    public String countAndSay(int n) {
        String str = "1";
        for (int i = 2; i <= n; ++i) {
            StringBuilder sb = new StringBuilder();
            int start = 0;
            int pos = 0;

            while (pos < str.length()) {
                // 相同的数字归为一组
                while (pos < str.length() && str.charAt(pos) == str.charAt(start)) {
                    pos++;
                }
                sb.append(Integer.toString(pos - start)).append(str.charAt(start));
                start = pos;
            }
            str = sb.toString();
        }

        return str;
    }

}
